package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

class StrokeCapView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //线宽
            strokeWidth = context.resources.getDimension(R.dimen.dp_5)
            //线头--圆头
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_673ab7)
            style = Paint.Style.FILL
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //线宽
            strokeWidth = context.resources.getDimension(R.dimen.dp_5)
            //线头--方头
            strokeCap = Paint.Cap.SQUARE
            color = context.resources.getColor(R.color.color_64C630)
            style = Paint.Style.FILL
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //线宽
            strokeWidth = context.resources.getDimension(R.dimen.dp_5)
            //线头--平头(默认值,会被削去两端)
            strokeCap = Paint.Cap.BUTT
            color = context.resources.getColor(R.color.color_B128C9)
            style = Paint.Style.FILL
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))

        canvas?.apply {
            drawLine(mWidth / 6.0F, mHeight / 4.0F, mWidth * 5 / 6.0F, mHeight / 4.0F, mPaint0)

            drawLine(mWidth / 6.0F, mHeight / 2.0F, mWidth * 5 / 6.0F, mHeight / 2.0F, mPaint1)

            drawLine(mWidth / 6.0F, mHeight * 3 / 4.0F, mWidth * 5 / 6.0F, mHeight * 3 / 4.0F, mPaint2)
        }
    }

}